/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-11-30
 * @Time  : 下午7:04
 * @brief :
 */
(function () {
    var util = require('common:widget/ui/libs/util.js').util;
    P.createPage({
        _data:{
            availableAmount: P.data('can_withdraw'),
            postData:{}
        },
        _create:function(){
            var Dialog = require('common:widget/ui/dialog/dialog.js').Dialog,
                $page = $('.page-withdraw'),
                $dropMenu = $page.find('.drop-menu'),
                dialog = new Dialog();

            this.data({
                '$page':$page,
                '$dropMenu':$dropMenu,
                '$bank':$page.find('.bank-list .self'),
                '$withdrawAmount':$page.find('.withdraw-amount'),
                '$iphoneCode':$page.find('.iphone-code'),
                '$tradePassword':$page.find('.trade-password'),
                'dialog':dialog
            });


        },

        _init:function(){
            var $page = this.data('$page'),
                $dropMenu = this.data('$dropMenu'),
                me = this;

            $page.find('.bank-list .self').click(function(e){
                e.stopPropagation();
                if($dropMenu.find('li').length){
                    if($dropMenu.is(':visible')){
                        $dropMenu.slideUp();
                    }else{
                        $dropMenu.slideDown();
                    }
                }
            });

            $('body').click(function(){
                $dropMenu.slideUp();
            });

            $dropMenu.find('li').click(function(e){
                e.stopPropagation();
                $page.find('.bank-list .self')
                    .attr('data-id',$(this).attr('data-id'))
                    .html('' +
                        '<i></i>' +
                        '<span class="name">'+($(this).attr('data-bank-type'))+'</span>' +
                        '<span class="">尾号</span>' +
                        '<span class="number">'+($(this).attr('data-number'))+'</span>');

                $dropMenu.slideUp();
            });

//            $dropMenu.find('li').eq(0).trigger('click');

            this.data('$withdrawAmount').on('blur',function(){
                 var amount = $.trim($(this).val());
                 me.checkAmount(amount);
            });


            $page.find('.btn-withdraw').click(function(){
                if(me.check()){
                    $.post(
                        '/pay/createwithdrawserial',
                        me.data('postData'),
                        function(ret,status){
                            if(ret.error_no == 0){
                                 location.href="/user/wallet";
                            }else{
                                 me.data('dialog').show(ret.error_message);
                            }
                        },'json'
                    );
                }
            });
        },

        checkAmount:function(amount){
            var $page = this.data('$page');
            $page.find('.amount-error').hide();
            $page.find('.actual-withdraw-amount').text('￥0');

            if(!/^[0-9]+(\.[0-9]{1,2})?$/.test(amount)){
                $page.find('.amount-error').show().text('提现金额错误(金额最多到小数点两位)');
                return false;
            }else if(parseFloat(amount)<=0){
                $page.find('.amount-error').show().text('提现金额必须大于0');
                return false;
            }
//            else
            else if(parseFloat(amount)> this.data('availableAmount')){
                $page.find('.amount-error').show().text('超过可提现金额');
                return false;
            }else if(parseFloat(amount)>50000){
                $page.find('.amount-error').show().text('单次提现不能超过五万');
                return false;
            }
            if(P.data('need_fee')) {
                if(parseFloat(amount)<=2){
                    $page.find('.amount-error').show().text('你的提现需要大于2元，每次提现第三方需收取2元手续费');
                    return false;
                }
                $page.find('.actual-withdraw-amount').text('￥'+(util.formatFloat(parseFloat(amount)-2,2)));
            } else{
                $page.find('.actual-withdraw-amount').text('￥'+(amount));
            }

            return true;
        },

        check:function(){
            var me = this,
                pass = true,
                $page = this.data('$page'),
                dialog = this.data('dialog'),
                bankId = me.data('$bank').attr('data-id'),
                amount = $.trim(me.data('$withdrawAmount').val()),
                code = $.trim(me.data('$iphoneCode').val()),
                tradePassword = $.trim(me.data('$tradePassword').val());
            $page.find('.error-tip').hide();

            if(!P.data('id_card')){
                dialog.show('' +
                    '<div>提现前，先进行<a class="text-link" href="/user/security">实名认证</a></div>' +
                    '');
                return false;
            }

            if(!bankId){
                pass = false;
                $page.find('.bankcard-error').show().text('未选择银行卡');
            }

            if(!this.checkAmount(amount)){
                pass = false;
            }

            if(!/^\d{6}$/.test(code)){
                $page.find('.code-error').show().text('验证码需要是6位的整数');
                pass = false;
            }

            if(tradePassword.length>16 || tradePassword.length<6){
                $page.find('.password-error').show().text('密码长度需要6~16位');
                pass = false;
            }

            this.data('postData',{
                bankcard_id:bankId,
                amount:amount,
                code:code,
                trade_passwd:tradePassword
            });
            return pass;
        }

    });

})();